package d4_proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyUtil {
    public static Star createProxy(Bigstar bigstar) {
        //ClassLoader loader用当前类的类加载器,Class<?>[] interfaces指定代理有哪些方法,InvocationHandler h指定代理对象的功能
        Star starProxy=(Star)Proxy.newProxyInstance(ProxyUtil.class.getClassLoader(),new Class[]{Star.class}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                //代理的事情
                if(method.getName().equals("sing")){
                    System.out.println("read money 20");
                }else if(method.getName().equals("dance")){
                    System.out.println("read money 300000");
                }
                return method.invoke(bigstar,args);
            }
        });

        return starProxy;
    }
}
